package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsAdmin;
import com.itqf.constants.AdminConstant;
import com.itqf.dto.AdminDto;
import com.itqf.mapper.DtsAdminMapper;
import com.itqf.service.DtsAdminService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * projectName: store_parent
 *
 * @author: 赵伟风
 * time: 2021/6/8 10:27
 * description:
 */

@Service
public class DtsAdminServiceImpl  implements DtsAdminService {

    @Autowired
    private DtsAdminMapper adminMapper;

    /**
     * 根据用户名,查询对应的用户!
     *
     * @param username
     * @return
     */
    @Override
    public DtsAdmin findAdminByUserName(String username) {

        DtsAdmin admin = adminMapper.queryAdmin(username);

        return admin;
    }

    @Override
    public PageInfo<DtsAdmin> findAdminByPage(AdminDto adminDto) {

        //1.设置分页数据
        /**
         * 参数1：当前页数
         * 参数2：页容量
         */
        PageHelper.startPage(adminDto.getPage(),adminDto.getLimit());

        //2.查询数据
        List<DtsAdmin> admins = adminMapper.queryAdminByPage(adminDto);

        //3.装配到pageInfo
        PageInfo<DtsAdmin> pageInfo = new PageInfo<>(admins);

        return pageInfo;
    }

    @Override
    public int removeAdminById(Integer id) {

        int rows = adminMapper.deleteAdminById(id);

        return rows;
    }

    @Override
    public int updateAdminById(DtsAdmin dtsAdmin) {

        //1.完善数据
        dtsAdmin.setUpdateTime(new Date());

        String password = dtsAdmin.getPassword();

        password = new Md5Hash(password, AdminConstant.ADMIN_SLAT,2).toString();

        dtsAdmin.setPassword(password);

        //2.修改
        int rows = adminMapper.updateAdminById(dtsAdmin);

        return rows;
    }

    @Override
    public int saveAdmin(DtsAdmin dtsAdmin) {


        //1.完善数据
        dtsAdmin.setUpdateTime(new Date());
        dtsAdmin.setAddTime(new Date());

        String password = dtsAdmin.getPassword();

        password = new Md5Hash(password, AdminConstant.ADMIN_SLAT,2).toString();

        dtsAdmin.setPassword(password);
        //2.添加

        int rows = adminMapper.save(dtsAdmin);


        return rows;
    }
}
